package net.neiquan.applibrary.numberpicker.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.neiquan.applibrary.R;
import net.neiquan.applibrary.numberpicker.entity.CityResult;


public class AlertCityPicker extends Dialog {

    protected AlertCityPicker(Context context) {
        super(context, R.style.AlertChooser);

        setOwnerActivity((Activity) context);
    }

    protected AlertCityPicker(Context context, int theme) {
        super(context, theme);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Window window = this.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setAttributes(params);
    }

    public static class Builder {

        private Context context;
        private AlertCityPicker chooser;
        private OnCitySelectListener onItemClickListener;
        private boolean cancelable = true;
        private boolean isCancel = true;// 保证只调用一次
        private CharSequence title;
        private float titleSize;
        private float cancelSize;
        private float confirmSize;
        private int titleColor;
        private int cancelColor;
        private int confirmColor;


        public Builder setOnCitySelectListener(OnCitySelectListener onItemClickListener) {
            this.onItemClickListener = onItemClickListener;
            return this;
        }

        public Builder(Context context) {
            chooser = new AlertCityPicker(context);
            this.context = context;
            init();
        }

        public Builder(Context context, int theme) {
            chooser = new AlertCityPicker(context, theme);
            this.context = context;
            init();
        }

        public Builder setTitleSize(float titleSize) {
            this.titleSize = titleSize;
            return this;
        }


        public Builder setCancelSize(float cancelSize) {
            this.cancelSize = cancelSize;
            return this;
        }


        public Builder setConfirmSize(float confirmSize) {
            this.confirmSize = confirmSize;
            return this;
        }


        public Builder setTitleColor(int titleColor) {
            this.titleColor = titleColor;
            return this;
        }

        public Builder setCancelColor(int cancelColor) {
            this.cancelColor = cancelColor;
            return this;
        }

        public Builder setConfirmColor(int confirmColor) {
            this.confirmColor = confirmColor;
            return this;
        }

        private void init() {
        }

        public Builder setTitle(CharSequence title) {
            this.title = title;
            return this;
        }

        public Builder setTitle(int titleRes) {
            this.title = context.getResources().getString(titleRes);
            return this;
        }


        public Builder setCancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }


        private AlertCityPicker create() {
            LinearLayout view = null;
            if (chooser == null)
                return null;
            final CityPicker cityPicker = new CityPicker(context);
            if (title == null) {

            } else {
                LayoutInflater inflater = (LayoutInflater) context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View layout = inflater.inflate(R.layout.pic_layout, null);
                view = (LinearLayout) layout.findViewById(R.id.root_ll);
                view.addView(cityPicker);
                TextView titleTv = (TextView) layout.findViewById(R.id.title_tv);
                titleTv.setText(title);
                TextView confirm_tv = (TextView) layout.findViewById(R.id.confirm_tv);
                TextView cancel_tv = (TextView) layout.findViewById(R.id.cancel_tv);
                if (titleColor != 0) {
                    //int s =context.getResources().getColor(R.color.transparent);
                    titleTv.setTextColor(titleColor);
                }
                if (cancelColor != 0) {
                    cancel_tv.setTextColor(cancelColor);
                }
                if (confirmColor != 0) {
                    confirm_tv.setTextColor(confirmColor);
                }
                if (titleSize != 0) {
                    titleTv.setTextSize(titleSize);
                }
                if (cancelSize != 0) {
                    cancel_tv.setTextSize(cancelSize);
                }
                if (confirmSize != 0) {
                    confirm_tv.setTextSize(confirmSize);
                }
                confirm_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        isCancel = false;
                        chooser.dismiss();
                    }
                });
                cancel_tv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        chooser.dismiss();
                    }
                });
            }

            chooser.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    if (onItemClickListener != null) {
                        if (title != null) {
                            if (isCancel)
                                return;
                        }
                        CityResult cityResult = new CityResult();
                        cityResult.setProviceName(cityPicker.getPovinceStr());
                        cityResult.setProviceCode(cityPicker.getPovinceCode());
                        cityResult.setCityName(cityPicker.getCityStr());
                        cityResult.setCityCode(cityPicker.getCityCode());
                        cityResult.setRegionName(cityPicker.getCounyStr());
                        cityResult.setRegionCode(cityPicker.getCounyCode());
                        onItemClickListener.endSelect(cityResult, isCancel);
                    }
                }
            });
            chooser.setContentView(title == null ? cityPicker : view);
            chooser.setCanceledOnTouchOutside(cancelable);
            return chooser;
        }

        public AlertCityPicker show() {
            create().show();
            return chooser;
        }
    }

    public interface OnCitySelectListener {
        /**
         * @param cityResult 城市结果
         * @param isCancel   是不是点击的取消，或者触摸外屏消失的
         */
        void endSelect(CityResult cityResult, boolean isCancel);
    }
}

